<template>
	<div>
		<el-form ref="form" :model="wasteInfo" :rules="rules" label-width="100px">
			<el-form-item label="产生源编码" prop="sourceCode">
				<el-input v-model="wasteInfo.sourceCode" style="width: 100%;"></el-input>
			</el-form-item>
			<el-form-item label="标识编号" prop="identityCode">
				<el-input v-model="wasteInfo.identityCode" style="width: 100%;"></el-input>
			</el-form-item>
			<el-form-item label="产生源名称" prop="sourceName">
				<el-input v-model="wasteInfo.sourceName" style="width: 100%;"></el-input>
			</el-form-item>
			<el-form-item label="固废" prop="entWasteId">
				<template scope="scope">
					<el-select @foucs="getWasteList" style="width: 100%;" v-model="wasteInfo.entWasteId" placeholder="请配置危废" filterable>
						<el-option :label="item.wasteCode + ' ' +item.wasteName" :value="item.id"
							v-for="(item,index) in wasteList"></el-option>
					</el-select>
				</template>
			</el-form-item>

			<el-form-item label="生产设施" prop="equipmentId">
				<template scope="scope">
					<el-select style="width: 100%;" v-model="wasteInfo.equipmentId" placeholder="请配置生产设施" filterable>
						<el-option :label="item.equipmentName + ' ' +item.equipmentNo" :value="item.id"
							v-for="(item,index) in equipmentBaseList" :key="item.id"></el-option>
					</el-select>
				</template>
			</el-form-item>

			<div style="text-align: center;">
				<el-button v-if="editData && editData.id" type="danger" @click="deleteThis">删除</el-button>
				<el-button type="primary" @click="saveOrUpdate">保存</el-button>
				<el-button @click="cancle">取消</el-button>
			</div>
		</el-form>
	</div>
</template>

<script>
	let that
	export default {
		props: ['entId', 'editData'],
		name: 'addWasteSource',
		components: {},
		data() {
			return {
				params: {},
				visible: false,
				wasteInfo: {},
				unitList: [],
				typeList: [{
						label: '非生产性产生源',
						value: 'NOT_PRODUCT_SOURCE'
					},
					{
						label: '生产性产生源',
						value: 'PRODUCT_SOURCE'
					},
					{
						label: '次生危废产生源',
						value: 'SECONDARY_SOURCE'
					},
					{
						label: '其他',
						value: 'OTHER'
					}
				],
				enterpriseId: '',
				rules: {
					sourceCode: [{
						required: true,
						message: '请输入产生源编码',
						trigger: 'blur'
					}],
					sourceName: [{
						required: true,
						message: '请输入产生源名称',
						trigger: 'blur'
					}],
					entWasteId: [{
						required: true,
						message: '请选择关联的危废',
						trigger: 'blur'
					}],
				},
				wasteList: [],
				outletList: [],
				currentSource: {},
				equipmentBaseList: []
			}
		},
		created: function() {},
		mounted: function() {
			that = this
			that.params.ticketId = that.$store.state.token

			this.getWasteList();

			that.ajax({
				url: '//productionEquipment/getOriginProductionEquipmentBaseList',
				data: {},
				success: function(data) {
					that.equipmentBaseList = data.data;
				}
			});

			if (that.editData) {
				//console.log(that.editData)
				let thisData = that.editData
				//thisData.entWasteId = thisData.wasteSourceRelationModel.entWasteId

				if (thisData.gasOutletId) thisData.outletType = "废气排口"
				if (thisData.waterOutletId) thisData.outletType = "废水排口"
				if (thisData.outletType) that.changeOutLetType(thisData.outletType)
				that.wasteInfo = thisData
				//console.log(that.wasteInfo)
			}
		},
		methods: {

			changeOutLetType(value) {
				let thisApi
				if (value == "废气排口") thisApi = "gasOutlet/getOriginalGasOutletBaseList"
				if (value == "废水排口") thisApi = "waterOutlet/getOriginalWaterOutletBaseList"

				that.ajax({
					url: thisApi + '?ticketId=' + that.params.ticketId,
					data: JSON.stringify({}),
					contentType: 'application/json',
					success: function(result) {
						if (result.status && result.data) {
							that.outletList = result.data
							that.$forceUpdate()
						}
					}
				})
			},

			cancle() {
				that.$emit("cancle", true)
			},

			//删除危废
			deleteThis() {
				this.$confirm('此操作将删除此数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					that.ajax({
						url: '/sw/entWasteSource/deleteSwEntWasteSource?ticketId=' + that.params.ticketId,
						data: {
							sourceId: that.editData.id
						},
						contentType: 'application/x-www-form-urlencoded',
						success: function(result) {
							if (result.status && result.data) {
								that.$emit("success", true)
							}
						}
					})
				})
			},


			getWasteList: function() {
				var _this = this;
				var param = {}
				that.ajax({
					url: '/sw/entWaste/getSwEntWasteModelList?ticketId=' + that.params.ticketId,
					contentType: 'application/json',
					data: JSON.stringify(param),
					success: function(result) {
						if (result.status == 1) {
							_this.wasteList = result.data
						}
					}
				})
			},

			saveOrUpdate: function() {
				var _this = this;
				_this.$refs['form'].validate(function(valid) {
					if (valid) {
						var param = {
							id: _this.wasteInfo.id,
							sourceCode: _this.wasteInfo.sourceCode,
							identityCode: _this.wasteInfo.identityCode,
							sourceName: _this.wasteInfo.sourceName,
							sourceType: _this.wasteInfo.sourceType,
							entWasteIdList: [_this.wasteInfo.entWasteId],
							waterOutletId: _this.wasteInfo.waterOutletId,
							gasOutletId: _this.wasteInfo.gasOutletId,
							equipmentId: _this.wasteInfo.equipmentId,
						}
						that.ajax({
							url: '/sw/entWasteSource/saveOrUpdateSwEntWasteSource?ticketId=' + that
								.params.ticketId,
							data: JSON.stringify(param),
							contentType: 'application/json',
							success: function(data) {
								that.data = that.$options.data()
								if (data.status == 1) {
									if (!param.id) {
										that.$emit("success", true)
									} else {
										that.$emit("success", true)
									}
									that.$message.success("保存成功！")
								} else {}
							}
						});
					}
				})
			},
			getWasteTable: function(val) {
				this.visible = true;
			},
			handleSelectionChange: function(val) {
				this.multipleSelection = val;
			},
			getUnitList: function() {
				var _this = this;
				that.ajax({
					url: '/codeType/listCodeValue.htm?ticketId=' + that.params.ticketId,
					data: {
						typeCode: 'UNIT_TYPE'
					},
					success: function(data) {
						if (data.status == 1) {
							_this.unitList = data.data;
						}
					}
				});
			},

		}
	}
</script>

<style>

</style>